// import { promises } from 'dns'
// import { json } from 'stream/consumers'
import config from './config'
// import { detail,list } from './mock'
import * as Mock from './mock'

// 定义常用的请求属性
const DEFAULT_REQUEST_OPTIONS = {
    url:'',
    data:{},
    header:{
        'Content-Type':'application/json'
    },
    method:'GEt',
    dataType:'json'
}

// 封装请求
let util = {
    isDev:config.isDev, //true
    log() {
        this.isDev && console.log(...arguments);
    },
    alert(title = '提示', content = config.defaultAlertMsg) {
        if(typeof content === 'object') {
            content = this.isDEV && JSON.stringify(content) || config.defaultAlertMsg
        }
        wx.showModal({
          title,
          content
        })
    },
    setStorageData(key, value= '', cb) {
        wx.setStorage({
            key,
            data:value,
            success() {
                cb && cb()
            }
        })
    },
    getStorageData(key, cb) {
        let self = this
        wx.getStorage({
            key,
            success(res) {
                cb && cb(res.data)
            },
            fail(err) {
                let msg = err.errMsg || ''
                if(/wx.getStorage:fail/.test(msg)) {
                    self.setStorageData(key)
                }
            }
        })
    },
    request(opt) {
      let options =  Object.assign({},DEFAULT_REQUEST_OPTIONS,opt)
      let {url,data,header,method,dataType,mock = false} = options
      let self = this
      return new Promise((resolve,reject) => {
        if(mock) {
            let res = {
                statusCode:200,
                data:Mock[url]
            }
            if(res && res.statusCode == 200 && res.data) {
                resolve(res.data)
            } else {
                self.alert('提示', res)
                reject(res)
            }
        }else {
            wxx.request({
                url,
                data,
                headr,
                method,
                dataType,
                success(res) {
                    if(res && res.statusCode == 200 && res.data) {
                        resolve(res.data)
                    } else {
                        self.alert('提示', res)
                        reject(res)
                    }
                },
                fail(err) {
                    self.log(err)
                    self.alert('提示',res)
                    reject(res)
                }
            })
        }
      })
    }
}

export default util